/*
 * Bunisoft the Open Source Communications Company
 * Copyright 2006, Bunisoft Inc.,
 *
 * Portions of this software are Copyright 2006, JBoss Inc., and 
 * individual contributors as indicated by the @authors tag.
 * See the copyright.txt in the distribution for a full listing of
 * individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.teremail.store.paged;

import java.util.List;

import org.teremail.store.ObjectUtil;
import org.teremail.store.StoreException;


/**
 * 
 * @author Michael Barker <mailto:mike@middlesoft.co.uk>
 * @version $Revision: 1.1 $
 */
public class MemoryBlob implements Blob {
    
    private final String id;
    private final int pageSize;
    private final List<byte[]> pages;

    public MemoryBlob(String id, int pageSize, List<byte[]> pages) {
        this.id = id;
        this.pageSize = pageSize;
        this.pages = pages;
    }
    
    public long getLength() {
        if (pages.size() == 0) {
            return 0L;
        } else {
            long fullPages = pages.size() - 1;
            long pageSize = this.pageSize;
            long lastPageSize = pages.get(pages.size() - 1).length;
            return (fullPages * pageSize) + lastPageSize;
        }
    }

    /**
     * @return Returns the id.
     */
    public String getId() {
        return id;
    }

    /**
     * @return Returns the pageSize.
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * @return
     */
    public int getNumPages() {
        return pages.size();
    }

    public String toString() {
        return ObjectUtil.toString(this);
    }

    private final static String SIZE_ERROR = "The size of the page %d, is greater than the blobs page size %d";
    
    /**
     * @see org.teremail.store.paged.Blob#addPage(byte[])
     */
    public void addPage(byte[] bs) {
        if (bs.length > pageSize) {
            throw new StoreException(String.format(SIZE_ERROR, bs.length, pageSize));
        }
        
        byte[] copy = new byte[bs.length];
        System.arraycopy(bs, 0, copy, 0, bs.length);
        
        pages.add(copy);
    }

    /**
     * @see org.teremail.store.paged.Blob#getPage(int)
     */
    public byte[] getPage(int i) {
        return pages.get(i);
    }

    public byte[] getLastPage() {
        return pages.get(pages.size() - 1);
    }

    public void replaceLastPage(byte[] newData) {
        pages.set(pages.size() - 1, newData);
    }
}
